var board = new Array();
var socre = 0;
var hasConflicted = new Array();

$(function(){
	//开始游戏
	newGame();
	$("#newGame").click(function(){
		newGame();
	});
});


function newGame(){
	//初始化棋盘
	init();

	//随机生成两个数字
	generateOneNumber();
	generateOneNumber();	
}

function init(){
	for(var i=0;i<4;i++){
		for(var j =0;j<4;j++){
			var gridCell = $("#gird-cell-"+i+"-"+j);
			gridCell.css({
				"left":getPosLeft(i,j),
				"top":getPosTop(i,j)
			});		
		}
	}
	for(var i=0;i<4;i++){
		board[i] = new Array();
		hasConflicted[i] = new Array();
		for(var j =0;j<4;j++){
			board[i][j] = 0;
			hasConflicted[i][j] = false;
		}
	}
	updateBoardView();
	score = 0;
	updateScore(score);
}

function updateBoardView(){
	$(".number-cell").remove();
	for(var i=0;i<4;i++){
		for(var j =0;j<4;j++){
			$("#grid").append('<div class="number-cell" id="number-cell-'+i+'-'+j+'"></div>');	
			var theNumberCell = $("#number-cell-"+i+"-"+j);
			if(board[i][j] == 0){
				theNumberCell.css({
					'width':0,
					'height':0,
					'left':getPosLeft(i,j)+50,
					'top':getPosTop(i,j)+50
				});
			}else{
				theNumberCell.css({
					'width':"100px",
					'height':"100px",
					'left':getPosLeft(i,j),
					'top':getPosTop(i,j),
					'background-color':getBackgroundColor(board[i][j]),
					'color':getNumberColor(board[i][j])
				});
				theNumberCell.text(board[i][j]);
			}
			hasConflicted[i][j] = false;
		}
	}	
}


function generateOneNumber(){
	if(nospace(board)){
		return false;
	}
	//随机一个位置
	var randX =  Math.floor(Math.random()*4);
	var randY = Math.floor(Math.random()*4);

	for(var i=0;i<50;i++){
		if(board[randX][randY] == 0){
			break;
		}
		randX =  Math.floor(Math.random()*4);
		randY = Math.floor(Math.random()*4);
	}
	for(var i=0;i<4;i++){
		for(var j=0;j<4;j++){
			if(board[i][j] == 0){
				randX = i;
				randY = j;
				break;
			}
		}
	}


	var randNumber = Math.random()<0.5?2:4;
	board[randX][randY] = randNumber;
	showNumberWithAnimation(randX,randY,randNumber);
	return true;
}


$(document).keydown(function(event){
	event.preventDefault();
	switch(event.keyCode){
		case 37://left
			if(moveLeft()){
				setTimeout("generateOneNumber()",210);
				setTimeout("isGameOver()",300);
			}
			break;
		case 38://up
			if(moveUp()){
				setTimeout("generateOneNumber()",210);
				setTimeout("isGameOver()",300);
			}
			break;
		case 39://right
			if(moveRight()){
				setTimeout("generateOneNumber()",210);
				setTimeout("isGameOver()",300);
			}
			break;
		case 40://down
			if(moveDown()){
				setTimeout("generateOneNumber()",210);
				setTimeout("isGameOver()",300);
			}
			break;
	}
})

function moveLeft(){
	if(!canMoveLeft(board)){
		return false;
	}

	//move left
	for(var i=0;i<4;i++){
		for(var j=1;j<4;j++){
			if(board[i][j] != 0){
				for(var k=0;k<j;k++){
					if(board[i][k] == 0 && noBlockHorizon(i,k,j,board)){
						showMoveAnimation(i,j,i,k);
						board[i][k] = board[i][j];
						board[i][j] = 0;
						break;  
					}else if(board[i][k] == board[i][j] && noBlockHorizon(i,k,j,board) && !hasConflicted[i][k]){
						showMoveAnimation(i,j,i,k);
						board[i][k] += board[i][j];
						board[i][j] = 0; 

						score += board[i][k];
						updateScore(score);

						hasConflicted[i][k] = true;
						break; 
					}
				}
			}
		}
	}
	setTimeout(function(){
		updateBoardView();
	},200);
	return true;
}


function moveRight(){
	if(!canMoveRight(board)){
		return false;
	}

	//move right
	for(var i=0;i<4;i++){
		for(var j=2;j>=0;j--){
			if(board[i][j] != 0){
				for(var k=3;k>j;k--){
					if(board[i][k] == 0 && noBlockHorizon(i,j,k,board)){
						showMoveAnimation(i,j,i,k);
						board[i][k] = board[i][j];
						board[i][j] = 0;
						break;  
					}else if(board[i][k] == board[i][j] && noBlockHorizon(i,j,k,board) && !hasConflicted[i][k]){
						showMoveAnimation(i,j,i,k);
						board[i][k] += board[i][j];
						board[i][j] = 0; 

						score += board[i][k];
						updateScore(score);

						hasConflicted[i][k] = true;
						break; 
					}
				}
			}
		}
	}
	setTimeout(function(){
		updateBoardView();
	},200);
	return true;
}

function moveUp(){
	if(!canMoveUp(board)){
		return false;
	}
	//move up
	for(var j=0;j<4;j++){
		for(var i=1;i<4;i++){
			if(board[i][j] != 0){
				for(var k=0;k<i;k++){
					if(board[k][j] == 0 && noBlockVertical(j,k,i,board)){
						showMoveAnimation(i,j,k,j);
						board[k][j] = board[i][j];
						board[i][j] = 0;
						break;
					}else if(board[k][j] == board[i][j] && noBlockHorizon(j,k,i,board) && !hasConflicted[k][j]){
						showMoveAnimation(i,j,k,j);
						board[k][j] += board[i][j];
						board[i][j] = 0; 

						score += board[k][j];
						updateScore(score);

						hasConflicted[k][j] = true;
						break;
					}
				}
			}
		}
	}
	setTimeout(function(){
		updateBoardView();
	},200);
	return true;
}

function moveDown(){
	if(!canMoveDown(board)){
		return false;
	}
	//move down
	for(var j=0;j<4;j++){
		for(var i=2;i>=0;i--){
			if(board[i][j] != 0){
				for(var k=3;k>i;k--){
					if(board[k][j] == 0 && noBlockVertical(j,i,k,board)){
						showMoveAnimation(i,j,k,j);
						board[k][j] = board[i][j];
						board[i][j] = 0;
						break;  
					}else if(board[k][j] == board[i][j] && noBlockHorizon(j,i,k,board) && !hasConflicted[k][j]){
						showMoveAnimation(i,j,k,j);
						board[k][j] += board[i][j];
						board[i][j] = 0; 

						score += board[k][j];
						updateScore(score);

						hasConflicted[k][j] = true;
						break; 
					}
				}
			}
		}
	}
	setTimeout(function(){
		updateBoardView();
	},200);
	return true;
}


function isGameOver(){
	if(nospace(board) && nomove(board)){
		gameOver();
	}
}

function gameOver(){
	alert('game over');
}